<?php

/**
 * This is the model class for table "bill_of_materials".
 *
 * The followings are the available columns in table 'bill_of_materials':
 * @property integer $item_id
 * @property integer $component_id
 * @property string $quantity
 *
 * The followings are the available model relations:
 * @property Items $item
 */
class BillOfMaterials extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return BillOfMaterials the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'bill_of_materials';
	}
	
	/**
	 * @return int the primary key of the table
	 */
	public function primaryKey()
	{
		return 'item_id';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('item_id, component_id, quantity, reference_id', 'required'),
			array('item_id, component_id', 'numerical', 'integerOnly'=>true),
			array('quantity', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('item_id, component_id, quantity, reference_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'item' => array(self::BELONGS_TO, 'Item', 'item_id'),
			'component' => array(self::BELONGS_TO, 'Item', 'component_id')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'item_id' => 'Item',
			'component_id' => 'Component',
			'quantity' => 'Quantity',
			'reference_id' => 'Reference Id'
		);
	}
	
	public function buildBillOfMaterials($itemId)
	{
		$bill_of_materials = $this->model()->findAllByPk($itemId);
		return $bill_of_materials;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('item_id',$this->item_id);
		$criteria->compare('component_id',$this->component_id);
		$criteria->compare('quantity',$this->quantity,true);
		$criteria->compare('reference_id',$this->reference_id);
		
		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function behaviors()
	{ 
		return array( 'LoggableBehavior'=> 'application.modules.auditTrail.behaviors.LoggableBehavior', ); 
	}
}